package com.pan.behavioral.template.read.impl;

import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;

public class ReadEnglishTxt extends BasicReadFile {

    private ReentrantLock reentrantLock;
    private byte[] buf;
    private int len = 0;
    {
        buf = new byte[1024];
    }

    public ReadEnglishTxt(String path) {
        super(path);
        if (!path.endsWith(".txt")){
            throw new RuntimeException("This isn't TXT file");
        }
    }


    @Override
    public void openFile() {
        super.openFile();
    }

    @Override
    public boolean hasNext() {

        if (super.inputStream == null) {
            throw new RuntimeException("Null File");
        }
        try {
            len = inputStream.read(buf);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return len<0?false:true;
    }

    @Override
    public String read() {
        String result = new String(buf,0,len);
        len = 0;
        return result;
    }
}
